#!/usr/bin/env bash

set -eou pipefail

BABASHKA_XMX=${BABASHKA_XMX:-"-J-Xmx4500m"}

if [ -z "$GRAALVM_HOME" ]; then
    echo "Please set GRAALVM_HOME"
    exit 1
fi

BABASHKA_VERSION=$(cat resources/BABASHKA_VERSION)

BABASHKA_JAR=${BABASHKA_JAR:-"target/babashka-$BABASHKA_VERSION-standalone.jar"}

if [[ ! -f "$BABASHKA_JAR" ]]
then
    echo "Please run script/uberjar first."
    exit 1
fi

if [[ ! -x "$GRAALVM_HOME/bin/native-image" ]]; then
    "$GRAALVM_HOME/bin/gu" install native-image
fi

export JAVA_HOME=$GRAALVM_HOME
export PATH=$GRAALVM_HOME/bin:$PATH

# because script/test cleans target during ci before the jar can we saved
cp target/metabom.jar .

BABASHKA_BINARY=${BABASHKA_BINARY:-"bb"}

args=("-jar" "$BABASHKA_JAR"
      "-H:Name=$BABASHKA_BINARY"
      "-H:+ReportExceptionStackTraces"
      # "-H:+PrintAnalysisCallTree"
      # "-H:+DashboardAll"
      # "-H:DashboardDump=reports/dump"
      # "-H:+DashboardPretty"
      # "-H:+DashboardJson"
      # "-H:ReportAnalysisForbiddenType=java.awt.Toolkit:Instantiated"
      "--verbose"
      "--no-fallback"
      "--native-image-info"
      "--install-exit-handlers"
      # --trace-class-initialization=jdk.internal.net.http.common.DebugLogger,jdk.internal.net.http.websocket.WebSocketImpl,jdk.internal.net.http.common.Utils
      "$BABASHKA_XMX"
      "--enable-preview"
      "-march=compatibility" # necessary for compatibility with older machines, e.g. see https://github.com/borkdude/deps.clj/actions/runs/6337277754/job/17212028399
      "-O1")

BABASHKA_STATIC=${BABASHKA_STATIC:-}
BABASHKA_MUSL=${BABASHKA_MUSL:-}

if [ "$BABASHKA_STATIC" = "true" ]; then
    if [ "$BABASHKA_MUSL" = "true" ]; then
        args+=("--static")
        args+=("--libc=musl"
               # see https://github.com/oracle/graal/issues/3398
               "-H:CCompilerOption=-Wl,-z,stack-size=2097152")
    else
        # see https://github.com/oracle/graal/issues/3737
        args+=("-H:+UnlockExperimentalVMOptions")
        args+=("-H:+StaticExecutableWithDynamicLibC")
    fi
fi

BABASHKA_FEATURE_HSQLDB=${BABASHKA_FEATURE_HSQLDB:-}

if [ "$BABASHKA_FEATURE_HSQLDB" = "true" ]; then
    args+=("-H:IncludeResources=org/hsqldb/.*\.properties" "-H:IncludeResources=org/hsqldb/.*\.sql")
fi

BABASHKA_FEATURE_POSTGRESQL=${BABASHKA_FEATURE_POSTGRESQL:-}

if [ "$BABASHKA_FEATURE_POSTGRESQL" = "true" ]; then
    args+=("--initialize-at-build-time=org.postgresql.PGProperty")
fi

BABASHKA_FEATURE_SELMER=${BABASHKA_FEATURE_SELMER:-}

if [ "$BABASHKA_FEATURE_SELMER" = "true" ]; then
    args+=("-H:IncludeResources=json.human.css")
fi

BABASHKA_LEAN=${BABASHKA_LEAN:-}

if [ "$BABASHKA_LEAN" = "true" ]
then
    # Remove all the default features, unless explicitly set to true:
    export BABASHKA_FEATURE_XML="${BABASHKA_FEATURE_XML:-false}"
    export BABASHKA_FEATURE_YAML="${BABASHKA_FEATURE_YAML:-false}"
    export BABASHKA_FEATURE_CSV="${BABASHKA_FEATURE_CSV:-false}"
    export BABASHKA_FEATURE_TRANSIT="${BABASHKA_FEATURE_TRANSIT:-false}"
    export BABASHKA_FEATURE_JAVA_TIME="${BABASHKA_FEATURE_JAVA_TIME:-false}"
    export BABASHKA_FEATURE_JAVA_NET_HTTP="${BABASHKA_FEATURE_JAVA_NET_HTTP:-false}"
    export BABASHKA_FEATURE_JAVA_NIO="${BABASHKA_FEATURE_JAVA_NIO:-false}"
    export BABASHKA_FEATURE_HTTPKIT_CLIENT="${BABASHKA_FEATURE_HTTPKIT_CLIENT:-false}"
    export BABASHKA_FEATURE_HTTPKIT_SERVER="${BABASHKA_FEATURE_HTTPKIT_SERVER:-false}"
    export BABASHKA_FEATURE_CORE_MATCH="${BABASHKA_FEATURE_CORE_MATCH:-false}"
    export BABASHKA_FEATURE_HICCUP="${BABASHKA_FEATURE_HICCUP:-false}"
    export BABASHKA_FEATURE_TEST_CHECK="${BABASHKA_FEATURE_TEST_CHECK:-false}"
    export BABASHKA_FEATURE_SPEC_ALPHA="${BABASHKA_FEATURE_SPEC_ALPHA:-false}"
    export BABASHKA_FEATURE_SELMER="${BABASHKA_FEATURE_SELMER:-false}"
    export BABASHKA_FEATURE_LOGGING="${BABASHKA_FEATURE_LOGGING:-false}"
    export BABASHKA_FEATURE_PRIORITY_MAP="${BABASHKA_FEATURE_PRIORITY_MAP:-false}"
fi

if [[ -z "${BABASHKA_SHA:-}" ]]
then
    sha=$(git rev-parse HEAD)
    if [[ $? -eq 0 ]]; then
        export BABASHKA_SHA=$sha
    fi
fi

detected_graalvm_version=$("$GRAALVM_HOME/bin/native-image" "--version")

if [[ $detected_graalvm_version == native-image\ 25* ]]; then
    args+=("--future-defaults=all")
fi

echo "$GRAALVM_HOME/bin/native-image" "${args[@]}" "$@"

"$GRAALVM_HOME/bin/native-image" "${args[@]}" "$@"

./"$BABASHKA_BINARY" describe
